package com.hummingbird.portal.controller;

import com.hummingbird.portal.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@Slf4j
public class HomeController {
    private static final GrantedAuthority STUDENT = new SimpleGrantedAuthority("ROLE_STUDENT");
    private static final GrantedAuthority TEACHER = new SimpleGrantedAuthority("ROLE_TEACHER");

    @GetMapping("/index.html")
    public ModelAndView index(@AuthenticationPrincipal UserDetails userDetails) {
        if (userDetails.getAuthorities().contains(TEACHER)) {
            return new ModelAndView("index_teacher");
        } else if (userDetails.getAuthorities().contains(STUDENT)) {
            // 凡是带跳转的return语句必须要放在if-el里，否则会报 `重复跳转` 错
            return new ModelAndView("index");
        } else {
            return null;
        }
    }

//    @GetMapping("/index_teacher.html")
//    public ModelAndView index_teacher() {
//        return new ModelAndView("index_teacher");
//    }

    // springboot + thymeleaf
    @GetMapping("/question/create.html")
    public ModelAndView create() {
        return new ModelAndView("question/create");
    }

    @GetMapping("/question/detail.html")
    public ModelAndView detail() {
        return new ModelAndView("question/detail");
    }
}
